import $http from '@/api/request.js'
import common from '@/common/common.js'
import wx from 'jweixin-module'
class Api {
	async scanCode() {
		return new Promise((resolve, reject) => {
			wx.scanQRCode({
				needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
				scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
				success: async function(res) {
					const result = res.resultStr
					const requestData = {
						code: result,
						shop_id: uni.getStorageSync('store_id'),
					}
					const response = await $http.get({
						url: '/singleSaleManageApi/scan',
						data: requestData
					})
					if (response.code != 200) {
						common.$Toast(response.msg)
						reject(new Error(response.msg))
					} else {
						const v_id = uni.getStorageSync('vid')
						if (v_id) uni.removeStorageSync('vid')
						resolve(response.data)
					}

				},
				fail: function(error) {
					reject(error)
				}
			})
		})
	}
}

export default new Api()